package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.dav.Env;
import com.fr.base.file.ConfigManager;
import com.fr.util.Consts;
import com.fr.web.core.HTMLWriterUtils;
import com.fr.web.core.WebUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegProcessorService extends NoSessionIDService
{
  private static RegProcessorService REGPROCESSOR_SERVICE = new RegProcessorService();

  public static RegProcessorService getInstance()
  {
    return REGPROCESSOR_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return ((paramString.equals("reg")) || (paramString.startsWith("reg_")));
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (paramString == null)
      return;
    paramString = paramString.toLowerCase();
    if ("reg".equals(paramString))
    {
      openRegPage(paramHttpServletRequest, paramHttpServletResponse);
    }
    else if ("reg_load".equals(paramString))
    {
      loadRegPane(paramHttpServletRequest, paramHttpServletResponse);
    }
    else if ("reg_intranet".equals(paramString))
    {
      regOnIntranet(paramHttpServletRequest, paramHttpServletResponse);
    }
    else
    {
      PrintWriter localPrintWriter;
      if ("reg_successfully".equals(paramString))
      {
        localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
        localPrintWriter.println("Registration successfully! please restart your server!");
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      else if ("reg_failed".equals(paramString))
      {
        localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
        localPrintWriter.println("Registration failed! please contact finereport!");
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      else if ("reg_diagnose".equals(paramString))
      {
        diagnose(paramHttpServletRequest, paramHttpServletResponse);
      }
    }
  }

  private static void openRegPage(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Object localObject;
    Env localEnv = FRContext.getCurrentEnv();
    InputStream localInputStream = null;
    if (localEnv != null)
      try
      {
        localInputStream = localEnv.readResource("FineReport.lic");
      }
      catch (Exception localException)
      {
        localInputStream = null;
      }
    if (localInputStream != null)
    {
      localObject = WebUtils.createPrintWriter(paramHttpServletResponse);
      ((PrintWriter)localObject).println("This FineReport has been registered!<br>FineReport.lic is under directory WEB-INF/resources/");
      ((PrintWriter)localObject).flush();
      ((PrintWriter)localObject).close();
    }
    else
    {
      localObject = WebUtils.createTemplateMap(paramHttpServletRequest);
      ((Map)localObject).put("charset", FRContext.getConfigManager().getServerCharset());
      TemplateUtils.dealWithTemplate("/com/fr/web/core/dialog/reg.html", paramHttpServletResponse, (Map)localObject);
    }
  }

  private static void loadRegPane(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws IOException
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    Tag localTag = HTMLWriterUtils.writeRegForm(paramHttpServletRequest);
    if (localTag != null)
    {
      localTag.writeHtml(localPrintWriter);
      localPrintWriter.flush();
      localPrintWriter.close();
    }
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void diagnose(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    byte[] arrayOfByte = FRCoreContext.getBytes();
    if ((arrayOfByte != null) && (arrayOfByte.length != 0))
    {
      PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
      Properties localProperties = new Properties();
      try
      {
        localProperties.load(new ByteArrayInputStream(arrayOfByte));
      }
      catch (IOException localIOException)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
      }
      localPrintWriter.println("version " + localProperties.getProperty("version"));
      localPrintWriter.println("macaddress " + BaseCoreUtils.matchMacAddress((String)localProperties.get("macaddress")));
      localPrintWriter.println("version " + ((String)localProperties.get("vt4")));
      localPrintWriter.flush();
      localPrintWriter.close();
    }
  }

  private static void regOnIntranet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "form_value");
    JSONObject localJSONObject = new JSONObject(str);
    localJSONObject.put("version", Consts.VERSION).put("macaddress", BaseCoreUtils.getMacAddress()).put("appname", paramHttpServletRequest.getContextPath().replaceAll("/", ""));
    paramHttpServletResponse.setHeader("Cache-Control", "public");
    paramHttpServletResponse.setHeader("Cache-Control", "max-age=180");
    paramHttpServletResponse.setContentType("application/octet-stream");
    paramHttpServletResponse.setHeader("extension", "txt");
    paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=RegistrationInformation.txt");
    try
    {
      ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
      localServletOutputStream.write(localJSONObject.toString().getBytes("UTF-8"));
      localServletOutputStream.flush();
      localServletOutputStream.close();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
  }

  private static void crypt(InputStream paramInputStream, OutputStream paramOutputStream, Cipher paramCipher)
    throws IOException, GeneralSecurityException
  {
    int i = paramCipher.getBlockSize();
    int j = paramCipher.getOutputSize(i);
    byte[] arrayOfByte1 = new byte[i];
    byte[] arrayOfByte2 = new byte[j];
    int k = 0;
    int l = 1;
    while (true)
    {
      while (true)
      {
        if (l == 0)
          break label78;
        k = paramInputStream.read(arrayOfByte1);
        if (k != i)
          break;
        int i1 = paramCipher.update(arrayOfByte1, 0, i, arrayOfByte2);
        paramOutputStream.write(arrayOfByte2, 0, i1);
      }
      l = 0;
    }
    if (k > 0)
      label78: arrayOfByte2 = paramCipher.doFinal(arrayOfByte1, 0, k);
    else
      arrayOfByte2 = paramCipher.doFinal();
    paramOutputStream.write(arrayOfByte2);
  }
}